using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace CatalogMigration
{
	/// <summary>
	/// Summary description for MigrationTables.
	/// </summary>
	public class frmMigrationTables : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		
		private string tableName;
		private System.Windows.Forms.CheckedListBox checkedListBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmMigrationTables(string migrationTable)
		{
			InitializeComponent();
			tableName = migrationTable;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.panel1 = new System.Windows.Forms.Panel();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.btnCancel);
			this.panel1.Controls.Add(this.btnOK);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel1.Location = new System.Drawing.Point(0, 253);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(320, 32);
			this.panel1.TabIndex = 2;
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(160, 8);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(80, 8);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 0;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// checkedListBox1
			// 
			this.checkedListBox1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.checkedListBox1.Location = new System.Drawing.Point(0, 0);
			this.checkedListBox1.Name = "checkedListBox1";
			this.checkedListBox1.Size = new System.Drawing.Size(320, 244);
			this.checkedListBox1.TabIndex = 3;
			this.checkedListBox1.SelectedIndexChanged += new System.EventHandler(this.checkedListBox1_SelectedIndexChanged);
			// 
			// frmMigrationTables
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(320, 285);
			this.Controls.Add(this.checkedListBox1);
			this.Controls.Add(this.panel1);
			this.Name = "frmMigrationTables";
			this.Text = "Migration Tables";
			this.Load += new System.EventHandler(this.frmMigrationTables_Load);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmMigrationTables_Load(object sender, System.EventArgs e)
		{
			// get the connection string and load the tables.
			string strConn, strSQL;
			strConn = frmCatalogMigrationTool.ConStr;
			strSQL = "Select name from sysobjects where xtype = 'U'";
			SqlConnection cn = new SqlConnection(strConn);
			cn.Open();
			SqlCommand cmd = cn.CreateCommand();
			cmd.CommandText = strSQL;
			cmd.Connection = cn;
			SqlDataReader rdr = cmd.ExecuteReader();

			while(rdr.Read())
			{
				checkedListBox1.Items.Add(rdr.GetValue(0).ToString());
			}

			cn.Close();
		}

		private void checkedListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// only allow one checkbox to be selected.
			// get the current selected item
			int currentChkItem = checkedListBox1.SelectedIndex;
			
			// iterate and uncheck all items
			for(int i = 0; checkedListBox1.Items.Count > i; i++)
			{
				checkedListBox1.SetItemChecked(i, false);
			}

			// only set what was selected
			checkedListBox1.SetItemChecked(currentChkItem, true);
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			if(tableName=="Products")
			{
				frmCatalogMigrationTool.productTable = checkedListBox1.SelectedItem.ToString();
				Tools.Log(@"[Info] Products table selected");
			}
			else if(tableName=="Categories")
			{
				frmCatalogMigrationTool.categoryTable = checkedListBox1.SelectedItem.ToString();
				Tools.Log(@"[Info] Categories table selected");
			}

			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
